package cn.yitun.chat.yitunserverimcore.general.common.function;

import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactRelationVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupMemberVo;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactRelationDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupMemberDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.userchat.manager.UserManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.user.UserPush;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yitun
 * @date 2019-01-20 10:28:51
 * @since 1.0
 */
@Component
public class UserUpdateFunction {

	@Resource
	private ContactRelationDao contactRelationDAO;
	@Resource
	private GroupMemberDao groupMemberDAO;
	@Resource
	private UserPush userPush;

	@Resource
	public void setUserManager(UserManager userManager) {
		//this.userManager = userManager;
		userManager.addUpdateDataEvent(userId -> {
			updateData(userId);
		});

		userManager.addUpdateHeadEvent(userId -> {
			updateHead(userId);
		});

		userManager.addUpdateChatOnlineEvent((userId, chatOnline) -> {
			updateChatOnlineStatus(userId, chatOnline);
		});

		userManager.addUpdateSignatureEvent((userId, signature) -> {
			updateSignature(userId, signature);
		});
	}

	/**
	 * 
	 * Description 用户修改信息后推送给所在群和联系人 <br>
	 * Date 2019-04-30 17:50:16<br>
	 * 
	 * @param userId void
	 * @since 1.0
	 */
	private void updateData(Long userId) {
		if (null != userId) {
			List<Long> list = getCorrelationUserIds(userId);
			userPush.pushUserUpdate(IdGen.getNumber(), userId, list);
		}
	}

	private void updateHead(Long userId) {
		if (null != userId) {
			List<Long> list = getCorrelationUserIds(userId);
			userPush.pushUserUpdateHead(IdGen.getNumber(), userId, list);
		}
	}

//	private void updateStatus(Long userId, String status) {
//		if (null != userId) {
//			List<Long> list = getCorrelationUserIds(userId);
//			userPush.pushUserStatus(IdGen.getNumber(), userId, status, list);
//		}
//	}
	private void updateChatOnlineStatus(Long userId, String chatOnline) {
		if (null != userId) {
			List<Long> list = getCorrelationUserIds(userId);
			userPush.pushUserStatus(IdGen.getNumber(), userId, chatOnline, list);
		}
	}

	private void updateSignature(Long userId, String signature) {
		if (null != userId) {
			List<Long> list = getCorrelationUserIds(userId);
			userPush.pushUserUpdateSignature(IdGen.getNumber(), userId, signature, list);
		}
	}

	private List<Long> getCorrelationUserIds(Long userId) {
		Set<Long> set = new HashSet<>();
		if (null != userId) {
			List<ContactRelationVo> contactList = contactRelationDAO.listVO(R.param("contactUserId", userId));
			List<GroupMemberVo> groupMemberList = groupMemberDAO.listVO(R.param("userId", userId));

			for (ContactRelationVo cr : contactList) {
				set.add(cr.getOwnerUserId());
			}
			for (GroupMemberVo gm : groupMemberList) {
				set.add(gm.getUserId());
			}
		}
		return new ArrayList<>(set);
	}
}
